package jw.hospital.controller.api;

import jw.hospital.common.exception.HospitalException;
import jw.hospital.common.result.ResultCodeEnum;
import jw.hospital.common.result.ResultResponse;
import jw.hospital.helper.HttpRequestHelper;
import jw.hospital.hosp.model.hospital.Department;
import jw.hospital.hosp.model.hospital.Hospital;
import jw.hospital.hosp.vo.DepartmentQueryVo;
import jw.hospital.service.DepartmentService;
import jw.hospital.service.HospitalService;
import jw.hospital.service.HospitalSetService;
import jw.hospital.utils.MD5;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping(value = "/api/hosp")
public class ApiController {
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService  departmentService;
    @Autowired
    private HospitalService  hospitalService;

    @PostMapping("department/remove")
    public   ResultResponse   removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> stringMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) stringMap.get("hoscode");
        String sign = (String) stringMap.get("sign");
        String depcode = (String) stringMap.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);
        if(!sign.equals(encrypt)){
            throw  new  HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return  ResultResponse.ok();

    }

    @PostMapping("department/list")
    public  ResultResponse  findDepartmentList(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> stringMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) stringMap.get("hoscode");
        Integer page =StringUtils.isEmpty((String) stringMap.get("page"))?1:Integer.parseInt((String) stringMap.get("page"));
        Integer limit =StringUtils.isEmpty((String) stringMap.get("limit"))?1:Integer.parseInt((String) stringMap.get("limit"));
        //验证签名
        String signKey = hospitalSetService.getSignKey("hoscode");
        String sign = (String) stringMap.get("sign");
        String encrypt = MD5.encrypt(signKey);
        if(!sign.equals(encrypt)){
            throw  new  HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo  departmentQueryVo =new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> departmentList  =departmentService.findPageDeptment(page,limit,departmentQueryVo);
        return  ResultResponse.ok(departmentList);


    }



    @PostMapping("show")
    public  ResultResponse<Hospital>  show(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        String sign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);
        if(!sign.equals(encrypt)){
            throw new  HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getHospitalByHosCode(hoscode);
        return  ResultResponse.ok(hospital);

    }

    @PostMapping("saveDepartment")
    public ResultResponse  saveDepartment(HttpServletRequest request){
        //获取提交表单数据的新方法request.getParameterMap。
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        //1.获取医院传递过来的签名
        String sign = (String) paramMap.get("sign");
        //2.根据医院编码查询签名
        String signKey =   hospitalSetService.getSignKey(hoscode);
        //3.将signKey进行MD5加密
        String encrypt = MD5.encrypt(signKey);
        //4.进行比较
        if(!sign.equals(encrypt)){
            throw  new HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return  ResultResponse.ok();

    }




    @PostMapping("saveHospital")
    public   ResultResponse  saveHospital(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object>  paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        //签名前端已经进行Md5加密
        String sign = (String) paramMap.get("sign");
        //1.通过hoscode查询signKey
        String signKey = hospitalSetService.getSignKey(hoscode);
        String encrypt = MD5.encrypt(signKey);  //保存的时候已经进行加密
        //2.对比加密的key是否一致
        if(!sign.equals(encrypt)){
            throw new  HospitalException(ResultCodeEnum.SIGN_ERROR);
        }
        //3.Base64对图片地址加密的时候会将"+"号变成”“,需要转换
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll("","+");
        paramMap.put("logodata",logoData);
        hospitalService.save(paramMap);
        return ResultResponse.ok();
    }

}
